package yxy.game.pm2.command.threelinemutualpush;

import org.joda.time.DateTime;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ThreeLineMutualPushMessage;
import yxy.game.pm2.module.threelinepush.ThreeLinePushModule;

import java.util.HashMap;
import java.util.Map;

/****************************************
 * @name ThreeLineMutualPushMonsterLevelUp
 * @description 三线互推小游戏怪物升级
 * @author 梁健彬
 * @since 2022-08-17
 ***************************************/
@MessageType(proto = Command.ThreeLineMutualPushMonsterLevelUp, classtype = ThreeLineMutualPushMessage.ThreeLineMutualPushMonsterLevelUpRequest.class)
public class CMD_ThreeLineMutualPushMonsterLevelUp extends Command<ThreeLineMutualPushMessage.ThreeLineMutualPushMonsterLevelUpRequest> {

    @Override
    public void execute(MessageSource messageSource, ThreeLineMutualPushMessage.ThreeLineMutualPushMonsterLevelUpRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        ThreeLineMutualPushMessage.ThreeLineMutualPushMonsterLevelUpResponse.Builder response = ThreeLineMutualPushMessage.ThreeLineMutualPushMonsterLevelUpResponse.newBuilder();
        long gold = request.getGold();
        int id = request.getId();

        try {
            ThreeLinePushModule module = ThreeLinePushModule.get(playerCode);

            if (module == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            if ((module.getGold() - gold) < 0) {
                send(messageSource, response, Result.ThreeLineMutualPushMonsterLevelCantUp);
                return;
            }

            Integer integer = module.getMonster().get(id);
            if (integer == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            module.getMonster().put(id, integer + 1);
            long num = module.getGold() - gold;
            module.setGold(num);
            module.set();

            response.setCurrentMapId(module.getCurrentMapId());
            response.addAllHasReceiveIds(module.getRewardIds());

            DateTime updateTime = module.getUpdateTime();
            long time = updateTime.getMillis();
            response.setUpdateTime(time);//上次更新时间
            response.setGold(module.getGold());//当前的金币数量

            HashMap<Integer, Integer> monster = module.getMonster();
            for (Map.Entry<Integer, Integer> entry : monster.entrySet()) {
                Integer key = entry.getKey();
                Integer value = entry.getValue();
                ThreeLineMutualPushMessage.ThreeLineMonster.Builder monsterBuilder = ThreeLineMutualPushMessage.ThreeLineMonster.newBuilder();
                monsterBuilder.setId(key);
                monsterBuilder.setLevel(value);
                response.addMonster(monsterBuilder);
            }

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);

        }
    }

    private void send(MessageSource messageSource, ThreeLineMutualPushMessage.ThreeLineMutualPushMonsterLevelUpResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }


}
